package com.software.thread;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;


public class MainActivity extends AppCompatActivity {

    private TextView tv_baidu;
    private Button btn_baidu;
    private ImageView iv_img;
    private Button btn_img;

    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        handler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(@NonNull Message msg) {
                //处理图像与控件绑定
                if (msg.what == 1) {
                    Bitmap bitmap = (Bitmap) msg.obj;
                    iv_img.setImageBitmap(bitmap);
                }
            }
        };
        //绑定访问百度页面的事件
        btn_baidu.setOnClickListener(v -> {
            new Thread(){
                @Override
                public void run() {
                    visitBaidu();
                }
            }.start();

        });

        //绑定下载头像的事件
        btn_img.setOnClickListener(v -> {
            new Thread(){
                @Override
                public void run() {
                    downloadImg("https://tse4-mm.cn.bing.net/th/id/OIP-C._EJ4gAHvtRGVBotZqOtd4gAAAA?w=193&h=193&c=7&r=0&o=5&dpr=1.1&pid=1.7");
                }
            }.start();
        });
    }

    private void downloadImg(String imgUrl) {
        URL url = null;
        InputStream is = null;
        try {
            url = new URL(imgUrl);
            is = url.openStream();
            //BitMapFactory 来构建BitMap图像
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            //设置控件图像
            //获取Message  建议使用获取的
            Message message = handler.obtainMessage();
            message.what = 1;
            message.obj = bitmap;
            //向消息队列发送消息
            handler.sendMessage(message);
            iv_img.setImageBitmap(bitmap);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    /**
     * 此方法在子线程中运行
     */
    private void visitBaidu() {
        URL url = null;
        InputStream is = null;
        try {
            //创建URL

            //访问网络需要获取系统权限
            url = new URL("https://www.baidu.com");
            //获得流(输入流)
            is = url.openStream();
            //通过循环读取流中的数据
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuffer sb = new StringBuffer();
            while (br.readLine() != null) {
                sb.append(br.readLine());

            }
            //将数据sb写入TextView  TextView在主线程中运行
            //要进行主子线程通讯
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv_baidu.setText(sb.toString());
                }
            });
        }catch (IOException e){
            e.printStackTrace();
        } finally {
            //关闭流

            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }




    }

    private void initViews() {
        btn_baidu = findViewById(R.id.btn_baidu);
        tv_baidu = findViewById(R.id.tv_baidu);
        btn_img = findViewById(R.id.btn_img);
        iv_img = findViewById(R.id.iv_img);
    }
}